package com.isoft.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.entity.FlightRouteChange;
import com.isoft.result.R;
import com.isoft.service.FlightRouteChangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("api/flightRouteChange")
public class FlightRouteChangeController {

    @Autowired
    private FlightRouteChangeService flightRouteChangeService;

    // 新增草稿
    @PostMapping
    public R add(@RequestBody FlightRouteChange flightRouteChange) {
        flightRouteChange.setApprovalStatus("DRAFT");
        flightRouteChange.setCreateTime(new Date());
        flightRouteChange.setUpdateTime(new Date());
        boolean saved = flightRouteChangeService.save(flightRouteChange);
        return saved ? R.ok("新增草稿成功") : R.error("新增草稿失败");
    }

    // 删除变更申请
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        FlightRouteChange change = flightRouteChangeService.getById(id);
        if (change == null) {
            return R.error("变更申请不存在");
        }

        // 只有草稿状态才能删除
        if (!"DRAFT".equals(change.getApprovalStatus())) {
            return R.error("只能删除草稿状态的变更申请");
        }

        boolean removed = flightRouteChangeService.removeById(id);
        return removed ? R.ok("删除成功") : R.error("删除失败");
    }

    // 修改变更申请
    @PutMapping
    public R update(@RequestBody FlightRouteChange flightRouteChange) {
        FlightRouteChange existing = flightRouteChangeService.getById(flightRouteChange.getId());
        if (existing == null) {
            return R.error("变更申请不存在");
        }

        // 只有草稿状态才能修改
//        if (!"DRAFT".equals(existing.getApprovalStatus())) {
//            return R.error("只能修改草稿状态的变更申请");
//        }

        flightRouteChange.setUpdateTime(new Date());
        boolean updated = flightRouteChangeService.updateById(flightRouteChange);
        return updated ? R.ok("修改成功") : R.error("修改失败");
    }

    // 查询单条变更申请
    @GetMapping("/{id}")
    public R get(@PathVariable Long id) {
        FlightRouteChange change = flightRouteChangeService.getById(id);
        return change != null ? R.ok(change) : R.error("未找到数据");
    }

    // 分页查询变更申请
    @GetMapping("/list")
    public R list(
            @RequestParam(defaultValue = "1") Integer pageNo,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String flightNumber,
            @RequestParam(required = false) String approvalStatus,
            @RequestParam(required = false) String applicantName
    ) {
        Page<FlightRouteChange> page = new Page<>(pageNo, pageSize);
        QueryWrapper<FlightRouteChange> queryWrapper = new QueryWrapper<>();

        if (flightNumber != null && !flightNumber.isEmpty()) {
            queryWrapper.like("FLIGHT_NUMBER", flightNumber);
        }
        if (approvalStatus != null && !approvalStatus.isEmpty()) {
            queryWrapper.eq("APPROVAL_STATUS", approvalStatus);
        }
        if (applicantName != null && !applicantName.isEmpty()) {
            queryWrapper.like("APPLICANT_NAME", applicantName);
        }

        queryWrapper.orderByDesc("CREATE_TIME");
        Page<FlightRouteChange> result = flightRouteChangeService.page(page, queryWrapper);
        return R.ok(result);
    }

    // 发起审批
    @PostMapping("/{id}/submit")
    public R submitForApproval(@PathVariable("id") Long id, @RequestBody FlightRouteChange changeWithNewData) {
        FlightRouteChange existing = flightRouteChangeService.getById(id);
        if (existing == null || !"DRAFT".equals(existing.getApprovalStatus())) {
            return R.error("只能对草稿状态的变更申请发起审批");
        }

        changeWithNewData.setId(id);
        boolean success = flightRouteChangeService.submitForApproval(changeWithNewData);
        return success ? R.ok("发起审批成功") : R.error("发起审批失败");
    }

    // 审核变更申请（超级管理员权限）
    @PostMapping("/{id}/approve")
    public R approveChange(
            @PathVariable Long id,
            @RequestParam Boolean approved,
            @RequestParam(required = false) String comments,
            @RequestParam Long approverId,
            @RequestParam String approverName
    ) {
        FlightRouteChange change = flightRouteChangeService.getById(id);
        if (change == null) {
            return R.error("变更申请不存在");
        }

        // 只有待审核状态才能审核
        if (!"PENDING".equals(change.getApprovalStatus())) {
            return R.error("只能审核待审核状态的变更申请");
        }

        boolean success = flightRouteChangeService.approveChange(id, approved, comments, approverId, approverName);
        return success ? R.ok("审核成功") : R.error("审核失败");
    }
}